﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.UI.WebControls;
using AutoMapper;
using TpWeb.Controllers;
using TpWeb.Domain.BizObj;
using System.IO.Compression;


namespace TpWeb.Models
{
    public class RegisterPersonViewModel
    {
        public int Id { get; set; }

        [Required]
        [Display(Name = "Имя")]
        public string FirstName { get; set; }

        [Required]
        [Display(Name = "Отчество")]
        public string MiddleName { get; set; }

        [Required]
        [Display(Name = "Фамилия")]
        public string LastName { get; set; }

        [Required]
        [Display(Name = "Эл. Почта")]
        [EmailAddress]
        public string Email { get; set; }

        [Required]
        [Display(Name = "Дата рождения")]
        [DataType(DataType.DateTime)]
        public DateTime BirthDate { get; set; }

        public string NationalityCode { get; set; }
        [Required]
        [Display(Name = "Гражданство")]
        public string Nationality { get; set; }

        [Required]
        [Display(Name = "Телефон основной")]
        public string Phone { get; set; }

        [Display(Name = "Телефон доп.")]
        public string Phone2 { get; set; }

        [Required]
        [Display(Name = "Номер, серия документа")]
        public string DocNum { get; set; }

        [Required]
        [Display(Name = "Дата выдачи")]
        [DataType(DataType.DateTime)]
        public DateTime DocDate { get; set; }

        [Required]
        [Display(Name = "Выдан")]
        public string DocIssue { get; set; }

        [Display(Name = "Код подразделения")]
        public string DocDepCode { get; set; }

        [Display(Name = "Копия (НЕобязательно)")]
        public HttpPostedFileBase DocScan { get; set; }
        public HttpPostedFileBase DocScan2 { get; set; }
        public HttpPostedFileBase DocScan3 { get; set; }
        public HttpPostedFileBase DocScan4 { get; set; }

        [Required]
        [Display(Name = "Секрентный вопрос")]
        public string SecretQuestion { get; set; }

        [Required]
        [Display(Name = "Ответ на секрет")]
        public string SecretAnswer { get; set; }

        [Display(Name = "Доп. сведения")]
        public string Descr { get; set; }

        /*public Contact ToContact()
        {
            Mapper.CreateMap<RegisterPersonViewModel, Contact>()
                .ForMember(d=>d.Id,o=>o.Ignore)
                .ForMember(d => d.Created, o => o.UseValue<DateTime>(DateTime.Now))
                .ForMember(d => d.Name, o => o.ResolveUsing(s => string.Join(" ", s.LastName, s.FirstName, s.MiddleName)))
                .ForMember(d => d.Phone, o => o.ResolveUsing(s => string.Join(", ", s.Phone, s.Phone2)));


            return Mapper.Map<Contact>(this);
        }*/

        public Person ToPerson()
        {
            Mapper.CreateMap<RegisterPersonViewModel, Person>()
                .ForMember(dest => dest.DocScan, opt => opt.Ignore());

            var person = Mapper.Map<Person>(this);

            var scans = new List<HttpPostedFileBase>(
                new[] { DocScan, DocScan2, DocScan3, DocScan4 }
                .Where(d => d != null));

            using (var ms = new MemoryStream())
            {
                using (var zip = new ZipArchive(ms, ZipArchiveMode.Create))
                {
                    foreach (var scan in scans)
                    {
                        var scanEntry = zip.CreateEntry(scan.FileName);

                        using (var entryStream = scanEntry.Open())
                            scan.InputStream.CopyTo(entryStream);
                    }
                }
                person.DocScan = ms.ToArray();
            }

            return person;
        }


    }
}